package com.dc.project.controller;

import com.dc.common.core.ActFowDto;
import com.dc.common.validated.QueryGroup;
import com.dc.common.validated.QueryPageGroup;
import com.dc.common.validated.UpdateGroup;
import com.dc.common.vo.ResultVo;
import com.dc.project.workflow.service.ActFlowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * @author zhuangcy
 * @date 2021/6/25
 * @description 任务处理控制类
 */
@RequestMapping("task")
@RestController
@Api(tags = "流程任务处理类")
public class ActFlowTaskController {
    @Autowired
    private ActFlowService actFlowService;

    /**
     * 获取用户待办任务
     *
     * @return
     */
    @ApiOperation("获取用户待办任务")
    @GetMapping("userTodoTask")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10", required = true)
    })
    @RequiresPermissions("flow:todoTask:query")
    public ResultVo getUserTodoTask(@Validated(QueryPageGroup.class) ActFowDto dto) {
        return ResultVo.success().data(actFlowService.getUserTodoTask(dto));
    }


    /**
     * 获取用户已办任务
     *
     * @return
     */
    @ApiOperation("获取用户已办任务")
    @GetMapping("userDoneTask")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10", required = true)
    })
    @RequiresPermissions("flow:doneTask:query")
    public ResultVo getUserDoneTask(@Validated(QueryPageGroup.class) ActFowDto dto) {
        return ResultVo.success().data(actFlowService.getUserDoneTask(dto));
    }

    /**
     * 获取流程实例审批历史记录信息
     *
     * @return
     */
    @ApiOperation("获取流程实例审批历史记录信息")
    @GetMapping("taskHistory")
    @RequiresPermissions(value = {"flow:doneTask:query", "flow:todoTask:query"}, logical = Logical.OR)
    public ResultVo getUserTaskHistory(@Validated(QueryGroup.class) ActFowDto dto) {
        return ResultVo.success().data(actFlowService.getUserTaskHistory(dto));
    }


    /**
     * 执行流程任务审批操作
     *
     * @return
     */
    @ApiOperation("执行流程任务审批操作")
    @PutMapping("completeTask")
    @RequiresPermissions(value = {"flow:doneTask:query", "flow:todoTask:query"}, logical = Logical.OR)
    public ResultVo completeTask(@Validated(UpdateGroup.class) @RequestBody ActFowDto dto) {
        actFlowService.completeTask(dto);
        return ResultVo.success();
    }

    /**
     * 驳回审批到起点操作
     *
     * @return
     */
    @ApiOperation("驳回审批到起点操作")
    @PutMapping("rejectTaskToFirst")
    @RequiresPermissions(value = {"flow:doneTask:query", "flow:todoTask:query"}, logical = Logical.OR)
    public ResultVo rejectTaskToFirst(@Validated(UpdateGroup.class) @RequestBody ActFowDto dto) {
        actFlowService.rejectTaskToFirst(dto);
        return ResultVo.success();
    }

    /**
     * 驳回审批到上一节点操作
     *
     * @return
     */
    @ApiOperation("驳回审批到上一节点操作")
    @PutMapping("rejectTaskToParent")
    @RequiresPermissions(value = {"flow:doneTask:query", "flow:todoTask:query"}, logical = Logical.OR)
    public ResultVo rejectTaskToParent(@Validated(UpdateGroup.class) @RequestBody ActFowDto dto) {
        actFlowService.rejectTaskToParent(dto);
        return ResultVo.success();
    }

    /**
     * 驳回审批到任意节点操作
     *
     * @return
     */
    @ApiOperation("驳回审批到任意节点操作")
    @PutMapping("rejectTaskToAny")
    @RequiresPermissions(value = {"flow:doneTask:query", "flow:todoTask:query"}, logical = Logical.OR)
    public ResultVo rejectTaskToAny(@Validated(UpdateGroup.class) @RequestBody ActFowDto dto) {
        actFlowService.rejectTaskToAny(dto);
        return ResultVo.success();
    }

    /**
     * 获取审批流程任务信息
     *
     * @return
     */
    @ApiOperation("获取审批流程任务信息")
    @GetMapping("flowData")
    @RequiresPermissions(value = {"flow:doneTask:query", "flow:todoTask:query"}, logical = Logical.OR)
    public ResultVo getTaskFlowData(@NotNull(message = "任务id不能为空") @RequestParam("taskId") String taskId) {
        return ResultVo.success().data(actFlowService.getTaskFlowData(taskId));
    }


}

